﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Web;
using System.Collections;
using log4net;

namespace QualityRpt.Dao
{
    public class CacheManager
    {
        static ILog logger = LogManager.GetLogger("CacheManager");
        static Cache httpCache = HttpRuntime.Cache;
        public static void Put(string cacheKey, object obj)
        {
			httpCache.Insert(cacheKey, obj, null, DateTime.Now.AddHours(2), System.Web.Caching.Cache.NoSlidingExpiration);
        }
        public static void Put(string cacheKey, object obj, int expMinute)
        {
			httpCache.Insert(cacheKey, obj, null, DateTime.Now.AddMinutes(expMinute),System.Web.Caching.Cache.NoSlidingExpiration);
        }
        public static object Get(string cacheKey)
        {
            object rtObj = httpCache.Get(cacheKey);
            if (rtObj != null)
            {
                //logger.Info(cacheKey+"从缓存中读取.");
            }
            return rtObj;
        }
        public static void RemoveByPrefix(string prefix)
        {
            IDictionaryEnumerator idc = httpCache.GetEnumerator();
            while (idc.MoveNext())
            {
                if (idc.Key.ToString().StartsWith(prefix))
                {
                    httpCache.Remove(idc.Key.ToString());
                }
            }
        }
        public static void Remove(string cacheKey)
        {
            if (httpCache.Get(cacheKey) != null)
            {
                httpCache.Remove(cacheKey);
            }
        }
        public static void RemoveAll()
        {
            IDictionaryEnumerator idc = httpCache.GetEnumerator();
            while (idc.MoveNext())
            {
                httpCache.Remove(idc.Key.ToString());
            }
        }
    }
}
